package model;

/**
 *
 * @author Dustin
 */
public class Table {
    public final static int DEFAULT_SIZE = 4;
    public final static int PLAYING_CARDS = 4;

    private boolean locked;
    private String name;
    private Deck deck = new Deck();
    private Player[] players = new Player[DEFAULT_SIZE];
    private Card[] cards = new Card[PLAYING_CARDS];

    /**
     * Constructs a new Table with given name
     * @param aName
     */
    public Table(String aName) {
        name = aName;
    }

    /**
     * Query to get the current amount of Players at this Table
     * @return
     */
    public int getAmountOfPlayers() {
        int amountOfPlayers = 0;
        for(int i = 0; i < DEFAULT_SIZE; i++) {
            if(players[i] != null) {
                amountOfPlayers++;
            }
        }
        return amountOfPlayers;
    }

    public boolean isLocked() {
        return locked;
    }

    public String getName() {
        return name;
    }

    public Deck getDeck() {
        return deck;
    }
    
    public Player getPlayer(int index) {
        return players[index];
    }

    public Player[] getPlayers() {
        return players;
    }
    
    public Card getCard(int index) {
        return cards[index];
    }

    public Card[] getCards() {
        Card[] openCards = {cards[0], cards[1], cards[2]};
        return openCards;
    }

    public void setLocked(boolean value) {
        locked = value;
    }

    public void setName(String aName) {
        name = aName;
    }

    public void setPlayer(int index, HumanPlayer aPlayer) {
        if(!locked) {
            players[index] = aPlayer;
        }
    }

    public void setCard(int index, Card c) {
        cards[index] = c;
    }

    public void resetCards() {
        for(int i = 0; i < PLAYING_CARDS; i++) {
            cards[i] = null;
        }
    }

    public void printTable() {
        for(int i = 0; i < Table.DEFAULT_SIZE; i++) {
            System.out.println("Player " + players[i].getName() + " cards:");
            for(int j = 0; j < HumanPlayer.HAND_SIZE; j++) {
                System.out.println(players[i].getCard(j));
            }
            
            System.out.println("Points: " + ScoreController.determinePoints(players[i].getHand()));
            System.out.println();       
        }

        System.out.println("Pot cards: ");
        for(int j = 0; j < Table.PLAYING_CARDS - 1; j++) {
            System.out.println(cards[j]);
        }
        
        System.out.println();
        System.out.println("Blind card: ");
        System.out.println(cards[Table.PLAYING_CARDS - 1]);
        System.out.println();
        System.out.println();
    }
}
